import pymysql
from pyecharts import options
from pyecharts.charts import Bar

#连接数据库
db=pymysql.connect(
    host="localhost",
    user="root",
    password="12345",
    database="crawlerdemo",
    charset="utf8"
)

cursor=db.cursor()
sql="select job_place,count(job_id) from job group by job_place order by count(job_id) desc limit 5"

number=[]
city=[]

try:
    cursor.execute(sql)
    res=cursor.fetchall()
    for i in res:
        number.append(i[1])
        city.append(i[0])
except Exception as e:
    print(e)
    print("查询出问题")
#创建柱状图
bar=Bar()
#
bar.add_xaxis(city)
#添加y轴的数据
bar.add_yaxis("单位/个",number)
bar.set_global_opts(title_opts=options.TitleOpts(title="五个城市的岗位数图",subtitle="单位/个"))
bar.render("pienum.html")

cursor.close()
db.close()